package cn.lds.j368.dsedu.contorllelr;

import cn.lds.j368.dsedu.dto.ResultJson;
import cn.lds.j368.dsedu.entity.Order;
import cn.lds.j368.dsedu.entity.Video;
import cn.lds.j368.dsedu.exceipeion.SysException;
import cn.lds.j368.dsedu.service.OrderService;
import cn.lds.j368.dsedu.service.VideoService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/video")
@EnableDiscoveryClient
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private OrderService orderService;

    private Logger log = LoggerFactory.getLogger(VideoController.class);

    @GetMapping("/list")
    public ResultJson<List<Video>> showVideo() throws SysException {
        return videoService.selectVideoAll();
    }

    @GetMapping("/{id}")
    public String shwoVideo(@PathVariable("id") Integer videoId, HttpServletRequest request) throws SysException {

        String serverInfo = request.getServerName() + ":" + request.getServerPort();

        log.info("serverInfo:{}",serverInfo);


//        Map<String,Object> resultMap = new HashMap<>();
//        resultMap.put("serverInfo",serverInfo);
//
//        resultMap.put("result",videoService.selectVideoById(videoId).getObj());

        log.info("data:{}",videoService.selectVideoById(videoId));

//        ResultJson result = ResultJson.bulidSuccess(videoService.selectVideoById(videoId));

        String resultJson = JSON.toJSONString(videoService.selectVideoById(videoId));

        return resultJson;
    }

    @PostMapping("/add_order")
    public ResultJson addOrder(@RequestBody Order order){
        return orderService.addOrder(order);
    }
}
